package com.wanli.storemobile.homepage;


import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wanli.storemobile.R;
import com.wanli.storemobile.base.BaseActivity;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.WithdrawListBean;
import com.wanli.storemobile.mine.adapter.WithdrawListAdapter;
import com.wanli.storemobile.mine.model.IMineModel;
import com.wanli.storemobile.mine.model.MineModelImpl;
import com.wanli.storemobile.widget.AppTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawListActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.view_line1)
    View viewLine1;
    @BindView(R.id.ll_all)
    LinearLayout llAll;
    @BindView(R.id.tv_success)
    TextView tvSuccess;
    @BindView(R.id.view_line2)
    View viewLine2;
    @BindView(R.id.ll_success)
    LinearLayout llSuccess;
    @BindView(R.id.tv_fail)
    TextView tvFail;
    @BindView(R.id.view_line3)
    View viewLine3;
    @BindView(R.id.ll_fail)
    LinearLayout llFail;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private IMineModel mineModel;
    private List<WithdrawListBean.DataBean.ItemListBean> beanList;
    private WithdrawListAdapter listAdapter;
    private int page = 1;
    private SkeletonScreen skeletonScreen;
    private String status = "-1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
    }

    private void initData() {
        mineModel = new MineModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new WithdrawListAdapter(beanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);

        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_1)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_withdraw_list)
                .show();

        requestData();
    }

    public void requestData() {
        mineModel.requestStoreWithdrawal(page, status, new DataCallBack<WithdrawListBean>() {
            @Override
            public void onSuccessful(WithdrawListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        requestData();
        refreshLayout.finishLoadMore(1000);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshData();
    }

    public void refreshData() {
        page = 1;
        beanList.clear();
        requestData();
        refreshLayout.finishRefresh(1000);
    }

    @OnClick({R.id.ll_all, R.id.ll_success, R.id.ll_fail})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_all:
                status = "-1";
                viewLine1.setVisibility(View.VISIBLE);
                viewLine2.setVisibility(View.INVISIBLE);
                viewLine3.setVisibility(View.INVISIBLE);
                tvAll.setTextColor(getResources().getColor(R.color.theme_color));
                tvSuccess.setTextColor(getResources().getColor(R.color.text_color_2));
                tvFail.setTextColor(getResources().getColor(R.color.text_color_2));
                refreshData();
                skeletonScreen.show();
                break;
            case R.id.ll_success:
                status = "9";
                viewLine1.setVisibility(View.INVISIBLE);
                viewLine2.setVisibility(View.VISIBLE);
                viewLine3.setVisibility(View.INVISIBLE);
                tvAll.setTextColor(getResources().getColor(R.color.text_color_2));
                tvSuccess.setTextColor(getResources().getColor(R.color.theme_color));
                tvFail.setTextColor(getResources().getColor(R.color.text_color_2));
                refreshData();
                skeletonScreen.show();
                break;
            case R.id.ll_fail:
                status = "10";
                viewLine1.setVisibility(View.INVISIBLE);
                viewLine2.setVisibility(View.INVISIBLE);
                viewLine3.setVisibility(View.VISIBLE);
                tvAll.setTextColor(getResources().getColor(R.color.text_color_2));
                tvSuccess.setTextColor(getResources().getColor(R.color.text_color_2));
                tvFail.setTextColor(getResources().getColor(R.color.theme_color));
                refreshData();
                skeletonScreen.show();
                break;
        }
    }
}
